package mars.mp3player.utils;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;

import android.net.Uri;
import android.os.Environment;

public class FileUtils {
	private static String SDPATH;

	public FileUtils() {
		SDPATH = Environment.getExternalStorageDirectory() + File.separator;
	}
	
	public String getSDPATH(){
		return this.SDPATH;
	}

	/**
	 * 
	 * @param path
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public int createSDCardFile(String path, String fileName)
			throws IOException {
		File file = new File(SDPATH + path);
		if (!file.exists()) {
			boolean flag = file.mkdirs();
			if (flag) {
				file = new File(SDPATH + path +File.separator+ fileName);
				if(file.createNewFile()){
					return 0;
				}else{
					return -1;
				}
			}
		} else {
			file = new File(SDPATH + path +File.separator+ fileName);
			if (file.exists()) {
//				if (file.delete()) {
//					file.createNewFile();
//				}
				return 1;
			} else {
				if(file.createNewFile()){
					return 0;
				}else{
					return -1;
				}
			}
		}
		return 0;
	}
	
	/**
	 * 
	 * @param path
	 * @throws Exception 
	 */
	public File[] getFileList(String path, final String matchName) throws Exception{
		File file=new File(SDPATH+File.separator+path);
//		FileFilter filter=new FileFilter() {
//			
//			@Override
//			public boolean accept(File pathname) {
//				return false;
//			}
//		};
		FilenameFilter nameFilter=new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String filename) {
				if(filename.endsWith(matchName)){
					return true;
				}
				return false;
			}
		};
		File[] matchFile=null;
		if(file.isDirectory()){
			matchFile = file.listFiles(nameFilter);
		}else{
			throw new Exception(SDPATH+File.separator+path+" 不是目录");
		}
		
		return matchFile;
	}

	/**
	 *
	 * @param path
	 * @param fileName
	 * @return
	 */
	public Uri getFileUri(String path, String fileName) {
		String filePath=SDPATH+path+File.separator+fileName;
		Uri uri=Uri.parse("file://"+filePath);
		return uri;
	}
	
	
}
